package com.sandu.lib_common

import android.content.ComponentName
import android.content.Intent
import android.content.ServiceConnection
import android.os.Bundle
import android.os.IBinder
import android.util.Log
import com.google.android.material.bottomnavigation.BottomNavigationView
import androidx.appcompat.app.AppCompatActivity
import androidx.navigation.findNavController
import androidx.navigation.ui.AppBarConfiguration
import androidx.navigation.ui.setupActionBarWithNavController
import androidx.navigation.ui.setupWithNavController
import com.sandu.lib_common.databinding.ActivityTestBinding
import com.sandu.voiceassistant.IMyAidlInterface
import me.jessyan.autosize.internal.CustomAdapt

class TestActivity : AppCompatActivity() {

    private lateinit var binding: ActivityTestBinding
    val TAG = this@TestActivity.toString()

    private var voiceService: IMyAidlInterface? = null
    private var connection = object : ServiceConnection {
        override fun onServiceConnected(name: ComponentName?, service: IBinder?) {
            Log.i("clark", "test aidl client onServiceConnected")
            voiceService = IMyAidlInterface.Stub.asInterface(service)
        }

        override fun onServiceDisconnected(name: ComponentName?) {
            Log.i("clark", "test aidl client onServiceDisconnected")
        }



    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding = ActivityTestBinding.inflate(layoutInflater)
        setContentView(binding.root)

        val navView: BottomNavigationView = binding.navView

        val navController = findNavController(R.id.nav_host_fragment_activity_main)
        // Passing each menu ID as a set of Ids because each
        // menu should be considered as top level destinations.
        val appBarConfiguration = AppBarConfiguration(
            setOf(
                R.id.navigation_home, R.id.navigation_dashboard, R.id.navigation_notifications
            )
        )
        setupActionBarWithNavController(navController, appBarConfiguration)
        navView.setupWithNavController(navController)
        bindService()
//        voiceService?.sendVoice("this is test")


    }

    private fun bindService() {
        val intent = Intent()
        intent.action = "com.sandu.voiceservice"//子模块中注册时 要求的action
        intent.setPackage("com.sandu.voiceassistant") //目标应用包名，注意不是子模块的
        val bound = bindService(intent, connection, BIND_AUTO_CREATE)
        if (bound) {
            Log.i(TAG, "test aidl client bindService succeed")
        } else {
            Log.i(TAG, "test aidl client bindService failed")
        }

    }


}